package com.edu.learning.concurrent.sample.mode3;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * @author ll
 * @Date 2017/10/25 15:58
 */
public class ScheduledExecutorServiceDemo {
    public static void main(String[] args) {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().
                setNameFormat("ScheduledExecutorServiceDemo-%d").build();
        ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(2, threadFactory);
        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("bi bi bi");
            }
        }, 1, 1, TimeUnit.SECONDS);

        scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("cancel bi");
                scheduledFuture.cancel(true);
                scheduledExecutorService.shutdown();
            }
        }, 6, TimeUnit.SECONDS);
    }
}
